
mise_en_forme=function(){
		// Accordion
		$("#accordion").accordion({ header: "h4" });
		// Button
		$("#button").button();
		$("button","#compte").button();
		$("#radioset").buttonset();
		// Tabs
		$('#tabs').tabs();
		$( "button", "#table_pizzas" ).button();
		$("button", "#commandes").button();
		$("#commandesList").accordion({ header: "h4" });
};
bouton_annuler_order=function(){
	$('button[class*="annuler-commande"]').click(function(){
		var order=$(this).attr('idcommande');
		$.post("ajaxConnector.php",{ action:'annuler-order',orderid:order},function(data) {
			if(data!=null){
				document.location='index.php';
			}
		});
	});
	$('#annuler-order').click(function(){
		$.post("ajaxConnector.php",{ action:'annuler-order-client'},function(data) {
			if(data!=null){
				document.location='index.php';
			}
		});
	});
};
bouton_modifier_order=function(){
	$('button[class*="status-suivant"]').click(function(){
		var order=$(this).attr('idcommande');
		$.post("ajaxConnector.php",{ action:'changement-order-status',orderid:order},function(data) {
			if(data!=null){
				document.location='index.php';
			}
		});
	});
};
bouton_commande_pizza=function(){
	$( "button", "#table_pizzas" ).click(function(){
		if($(this).hasClass('button_order_pizza')){
			var idpizza=$(this).attr('idpizza');	
			$.post("ajaxConnector.php",{ action:'commander-pizza',pizzaid:idpizza},function(data) {
				if(data!=null){
					$('div#compte').html(data);
					$("button","#compte").button();
					valider_commande();
					alert('ajoutée au panier');
				}
			});
		}
	});
};
valider_commande=function(){
	$("#valider-commande").click(function(){
		$.post("ajaxConnector.php",{ action:'valider-commande'},function(data) {
			alert(data);
			if(data.indexOf('ok')!=-1){
				alert("changements pris en compte");
			}
			else{
				alert("une erreur s'est produite");
			}
			 document.location='index.php';
		});
	});
};
valider_modif=function(){
	var str=new Array();
	$('button[class*="valider"]').click(function(){
		$("#modifingredients option:selected").each(function(){
			str.push($(this).attr('ingredientid'));
		});
		var idpizza=$(this).attr('idpizza');
		var base = $('input[idpizza="'+idpizza+'"][name="nombase"]').val();
		var nom = $('input[idpizza="'+idpizza+'"][name="nompizza"]').val();
		var prix = $('input[idpizza="'+idpizza+'"][name="prix"]').val();
		$.post("ajaxConnector.php",{ action:'valider-modif-pizza',pizzaid:idpizza,base:base,prix:prix,nom:nom,ingredients:str},function(data) {
			if(data.indexOf('ok')!=-1){
				alert("changements pris en compte");
			}
			else{
				alert("une erreur s'est produite");
			}
			 document.location='index.php';
		});
	});
	$('button[class*="annuler"]').click(function(){
		document.location='index.php';
	});
};
modif_pizza=function(){
	$( "button", "#table_pizzas" ).click(function(){
		var idpizza=$(this).attr("idpizza");
		if($(this).hasClass('button_modif_pizza')){
			$.post("ajaxConnector.php",{ action:'modifier-pizza',pizzaid:idpizza},function(data) {
				if(data!=null){
					$('div#pizzas').html(data);
					$( "button", "#table_pizzas" ).button();
					modif_pizza();
				}
			});
			}
		});
};
button_modif_pizza=function(){
	$( "button", "#table_pizzas" ).click(function(){
		var idpizza=$(this).attr("idpizza");
		if($(this).hasClass('button_modif_pizza')){
			$.each($(this).parent().parent().find('td[class!="bouton-pizza"]'), function(index, value) {
			    $(this).html('<input type="text" name="'+$(this).attr('content')+'" value="'+$(this).html()+'" idpizza="'+idpizza+'"/>');
			});
			var ingredients = $('td[content="ingredients"][idpizza="'+idpizza+'"]').text();
			var tab=ingredients.split('  ');
			var html='<form>';
			for(var i=0;i<tab.length-1;i++){
				html+='<input type="checkbox" name="ingredient" value="'+tab[i]+'"/>'+tab[i];
			}
			html+="<form>";
			$('td[content="ingredients"][idpizza="'+idpizza+'"]').html(html);
			//$('td[content="ingredients"][idpizza="'+idpizza+'"]').html('<textarea class="textarea" idpizza="'+idpizza+'" content="ingredients">'+$('td[content="ingredients"][idpizza="'+idpizza+'"]').html()+'</textarea>');
			$(this).button("destroy");
			$(this).hide();
			$(this).parent().html('<button idPizza="'+idpizza+'" class="valider">valider</button><button idPizza="'+idpizza+'" class="annuler">annuler</button>');
			$( "button", "#table_pizzas" ).button();
			$('button[class*="valider"').click(function(){
				var idpizza=$(this).attr('idpizza');
				var base = $('input[idpizza="'+idpizza+'"][name="nombase"]').val();
				var nom = $('input[idpizza="'+idpizza+'"][name="nomPizza"]').val();
				var prix = $('input[idpizza="'+idpizza+'"][name="prix"]').val();
				$.post("ajaxConnector.php",{ action:'modifier-pizza',pizzaid:idpizza,base:base,prix:prix,nom:nom},function(data) {
					if(data=='ok'){
						alert("utilisateur a bien été créé");
						$(this).dialog("close");
					}
					else{
						alert("une erreure est survenue");
						$(this).dialog("close");
					}
				});
			});
		}
	});
};
login_register = function(){
	$( "#dialog:ui-dialog" ).dialog( "destroy" );
	var	email = $( "#register-email" ),
		password = $( "#register-password" ),
		allFields = $( [] ).add( name ).add( email ).add( password ),
		tips = $( ".validateTips" );

	function updateTips( t ) {
		tips
			.text( t )
			.addClass( "ui-state-highlight" );
		setTimeout(function() {
			tips.removeClass( "ui-state-highlight", 1500 );
		}, 500 );
	}

	function checkLength( o, n, min, max ) {
		if ( o.val().length > max || o.val().length < min ) {
			o.addClass("ui-state-error");
			updateTips("Length of " + n + " must be between"+
					min+" and "+max+".");
			return false;
		} else {
			return true;
		}
	}

	function checkRegexp( o, regexp, n ) {
		if ( !( regexp.test( o.val() ) ) ) {
			o.addClass("ui-state-error");
			updateTips( n );
			return false;
		} else {
			return true;
		}
	}
	$('#dialog').dialog({
		autoOpen: false,
		height: 300,
		width: 350,
		modal: true,
		buttons: {
		"Create": function() {
			var bValid = true;
			allFields.removeClass("ui-state-error");
			bValid = bValid && checkLength( email, "email", 6, 80 );
			bValid = bValid && checkLength( password, "password", 5, 16 );
			// From jquery.validate.js (by joern), contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
			bValid = bValid && checkRegexp( email, /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i, "eg. ui@jquery.com" );
			bValid = bValid && checkRegexp( password, /^([0-9a-zA-Z])+$/, "Password field only allow : a-z 0-9" );

		if ( bValid ) {
			$.post("ajaxConnector.php",{ action:'register',username:email.val(),password:password.val() },function(data) {
				if(data=='ok'){
					alert("utilisateur a bien été créé");
					$(this).dialog("close");
				}
				else{
					alert("une erreure est survenue");
					$(this).dialog("close");
				}
			});
		}
		$(this).dialog("close");
	},
	Cancel: function() {
		$(this).dialog("close");
		}
	},
	close: function() {
		allFields.val("").removeClass( "ui-state-error" );
		}
	});
	// Dialog Link
	$("#login-user").button();
	$("#login").submit(function(){
		$.post("ajaxConnector.php",{ action:"login",login:$('#username').val(),password:$("#password").val() },function(data) {
				document.location='index.php';
		});
		return false;
	});
	$("#register-user").button();
	$('#register-user').click(function(){
		$('#dialog').dialog('open');
		return false;
	});
};
affiche=function(){
	$( "button", "#table_pizzas" ).button();
	$("#destroy").button();
	$("#destroy").click(function(){
		$.post("ajaxConnector.php",{action:"logout", destroy:"ok" },function(data) {
		 document.location='index.php';
		});
	});
};